package cn.ekgc.bmdsad.day7;

import java.util.*;

public class JiFenSys {
    private static Scanner input = new Scanner(System.in);
    private static List<MyMember> members = new ArrayList<>();
    public static void main(String[] args) {
        while (true){
            //显示菜单
            mymenu(); //1.积分累计       2.积分兑换       3.查询剩余积分       4.修改密码       5.开卡       6.退出
            //做出选择
            if(choice()) break;
        }
        System.out.println("感谢使用");
    }

    private static boolean choice() {
        String key = input.nextLine();
        //就退出
        if("6".equals(key)){
            return true;
        }
        //处理
        operator(key);
        return false;

    }

    private static void operator(String key) {
        if("1".equals(key)){
            //积分累计
            saveScore();
            return;
        }
        if("2".equals(key)){
            //2.积分兑换
            duihuanScore();
            return;
        }
        if("3".equals(key)){
            showScore();
            return;
        }
        if("4".equals(key)){
            xiugaiPwd();
            return;
        }
        if("5".equals(key)){
            kaika();
            return;
        }
        System.out.println("请输入1~6之间的正整数");
    }

    private static void xiugaiPwd() {
        System.out.println("请输入您的会员卡号：");
        String cardId = input.nextLine();
        if (hasCardId(cardId)&&hasPwd(getPwd())){
            System.out.print("修改密码：");
            MyMember member = findMember(cardId);
            member.setPwd(getPwd());
            System.out.println("修改密码成功");
            return;

        }
        System.out.println("卡号或密码错误，无法修改密码");
    }


    private static void duihuanScore() {
        System.out.println("请输入您的会员卡号：");
        String cardId = input.nextLine();
        if (hasCardId(cardId)&&hasPwd(getPwd())){
            MyMember member = findMember(cardId);
            System.out.println("请输入您要此次消费使用低换的积分");
            int score = input.nextInt();
            if (score<=member.getScore()){
                member.setScore(member.getScore()-score);
                System.out.println("您此次抵消"+score/100*0.1+"元");
                System.out.println("积分兑换成功");
                return;
            }else{
                System.out.println("抱歉，积分不足，无法兑换");
                return;
            }
        }
        System.out.println("您输入的会员卡号或密码错误，无法积分兑换");
    }

    private static void showScore() {
        System.out.println("请输入您的会员卡号：");
        String cardId = input.nextLine();
        if (hasCardId(cardId)){
            MyMember member = findMember(cardId);
            System.out.println(member.getScore());
            return;
        }
        System.out.println("您输入的会员卡号错误，无法查询积分");
    }

    private static MyMember findMember(String cardId) {
        for (MyMember m:members){
            if (m.getCardId().equals(cardId)){
                return m;
            }
        }
        return null;
    }

    private static void kaika() {
        System.out.println("请输入注册姓名");
        String name = input.nextLine();
        String cardId = genCardId();
        String pwd = getPwd();
        Date date = new Date();
        //创建会员对象
        MyMember member = new MyMember(name,cardId,pwd,100,date);
        System.out.println(member);
        members.add(member);
    }

    private static String getPwd() {
        String pwd = null;
        while (true){
            System.out.println("请输入您的会员卡密码：");
            pwd = input.nextLine();
            if (pwd.length()>=6) break;
            System.out.println("你输入的太短");
        }
        return pwd;
    }

    private static String genCardId() {
        Random random = new Random();
        /*for (MyMember member:members){
            if (member.getCardId()==id){
                id = random.nextInt(99999999);
            }
        }*/
        String idstr = null;
        while (true){
            int id = random.nextInt(99999999);
            idstr = id+"";
            if (!hasCardId(idstr)) break;
        }
        return idstr;
    }

    private static boolean hasCardId(String id) {
        for (MyMember m: members){
            if (m.getCardId().equals(id)) return true;
        }
        return false;
    }

    private static void saveScore() {
        System.out.println("请输入您的会员卡号：");
        String cardId = input.nextLine();
        if (hasCardId(cardId)&&hasPwd(getPwd())){
            System.out.println("请输入您此次消费金额（1元积一分）");
            int score = input.nextInt();
            MyMember member = findMember(cardId);
            member.setScore(member.getScore()+score);
            System.out.println("积分累计成功");
            return;
        }
        System.out.println("您输入的会员卡号或密码错误，无法积分累计");
    }

    private static boolean hasPwd(String pwd) {
        for (MyMember m: members){
            if (m.getPwd().equals(pwd)) return true;
        }
        return false;
    }


    private static void mymenu() {
        System.out.println("*************************欢迎进入超市会员管理系统*************************");
        System.out.println("1.积分累计       2.积分兑换       3.查询剩余积分       4.修改密码       5.开卡       6.退出");
        System.out.println("*******************************************************************");
        System.out.print("请选择：");
    }
}
